//
//  PresentProjectMainCotroller.m
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/3/8.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "PresentProjectMainCotroller.h"


#import "SideBarViewController.h"
#import "NewHomepageViewController.h"
#import "indexViewController.h"
#import "ConnectUsViewController.h"
#import "PersonInfoViewController.h"
#import "ExhibitionProfileViewController.h"
#import "ExhibitorElectronicJournalViewController.h"
#import "BusinessMatchMakingViewController.h"
#import "XVCCollectPocket.h"
#import "XVCMessage.h"
#import "XVCVisitorMessage.h"
#import "XVCScanQRCode.h"
#import "XVCMapTypeShow.h"

#import "XVCReleaseProduct.h"

#import "SelectRegistrationStatusViewController.h"


#import "XVCProjectMainUse.h"

#define AppLanguage @"appLanguage"
#define CustomLocalizedString(key, comment) \
[[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"appLanguage"]] ofType:@"lproj"]] localizedStringForKey:(key) value:@"" table:nil]

@interface PresentProjectMainCotroller ()<SideBarViewControllerDelegate,UIAlertViewDelegate,NewHomepageViewControllerDelegate>

@property (nonatomic,strong) WebController *exhibitorWeb;

@property (nonatomic,strong) WebController *visitorWeb;

@end

@implementation PresentProjectMainCotroller
{
    UIViewController *sider;
}

@synthesize main;

@synthesize viewArray;

-(WebController*)exhibitorWeb
{
    if (_exhibitorWeb==nil)
    {
        _exhibitorWeb = [[WebController alloc] init];
        _exhibitorWeb.type = WebControllerRootVC;
        _exhibitorWeb.navTit = NSLocalizedString(@"展位预订", nil);
    }
    return _exhibitorWeb;
}

-(WebController*)visitorWeb
{
    if (_visitorWeb==nil)
    {
        _visitorWeb = [[WebController alloc] init];
        _visitorWeb.type = WebControllerRootVC;
        _visitorWeb.navTit = NSLocalizedString(@"观众预登记", nil);
    }
    return _visitorWeb;
}

-(instancetype)init
{
    self = [super init];
    if (self)
    {
        //main = [[MainViewController alloc] initWithRootViewController:[[indexViewController alloc] init]];
        NewHomepageViewController *nh=[[NewHomepageViewController alloc] init];
        //nh.delegate=self;
        main = [[MainViewController alloc] initWithRootViewController:nh];
        viewArray = [[NSMutableDictionary alloc] init];
    }
    return self;
}

+(void)getProjectMainControllerWithSoureViewController:(UIViewController*)sourceVC showTipType:(PresentProjectMainCotrollerType)type
{
    //注册JPUSH Tags、Alias
    NSString *userId = [[NSUserDefaults standardUserDefaults] stringForKey:userIDString];
    [JPUSHService setTags:[NSSet setWithArray:@[@"tag1"]]
               completion:^(NSInteger iResCode, NSSet *iTags, NSInteger seq) {
                   NSLog(@"标签设置成功");
               } seq:0];
    if (userId != nil &&
        ![userId isEqualToString:@""])
    {
        [JPUSHService setAlias:[NSString stringWithFormat:@"ppkc16%@",userId]
                    completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
                        NSLog(@"别名设置成功");
                    } seq:0];
    }
    
    //PresentProjectMainCotroller *use = [[PresentProjectMainCotroller alloc] init];

//    SideBarViewController *leftBar = [[SideBarViewController alloc] init];
//    leftBar.delegate = use;
   // use.main = [[MainViewController alloc] initWithRootViewController:[[NewHomepageViewController alloc] init]];
       //use.main = [[MainViewController alloc] initWithRootViewController:[[indexViewController alloc] init]];
    
//    WWSideslipViewController *side = [[WWSideslipViewController alloc] initWithLeftView:nil
//                                                                            andMainView:use.main
//                                                                           andRightView:nil
//                                                                     andBackgroundImage:[XXTool createImageWithColor:[UIColor colorWithRed:38/225.0 green:38/225.0 blue:38/255.0 alpha:0]]];
//    
//    side.sideslipTapGes.enabled = NO;
    
    NewHomepageViewController *nh=[[NewHomepageViewController alloc] init];
    
   MainViewController *main = [[MainViewController alloc] initWithRootViewController:nh];
    
    [sourceVC presentViewController:main animated:NO completion:^{
        if (type==PresentProjectMainCotrollerLogin)
        {
            [SVProgressHUD showSuccessWithStatus:NSLocalizedString(@"登录成功", nil)];
        }
        else if (type==PresentProjectMainCotrollerSubmit)
        {
            [SVProgressHUD showSuccessWithStatus:NSLocalizedString(@"注册成功", nil)];
        }
        else if (type==PresentProjectMainCotrollerNeedLogin)
        {
            [main presentViewController:[[MainViewController alloc] initWithRootViewController:[[SelectRegistrationStatusViewController alloc] init]] animated:YES completion:^{
            }];
        }
    }];
}

-(BOOL)sideBarView:(SideBarViewController *)view didClickIndex:(NSInteger)index clickThisBefore:(NSInteger)beforeindex isNeedSpecialTreat:(BOOL)is
{
    sider = view;
    [viewArray setObject:main.viewControllers forKey:[NSString stringWithFormat:@"%ldPage",(long)beforeindex]];
    
    //个人中心、商贸配对、消息、收藏夹、联系我们 需要判断是否登录
    if (index==11||index==3||index==4||index==5||index==9)
    {
        if ([[UserInfo shareUserInfo].userID isEqualToString:@""])
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"提示", nil)
                                                            message:NSLocalizedString(@"您还没有登录,请先登录", nil)
                                                           delegate:self
                                                  cancelButtonTitle:NSLocalizedString(@"取消", nil)
                                                  otherButtonTitles:NSLocalizedString(@"登录", nil), nil];
            [alert show];
            return NO;
        }
    }
    
    /*
    if (index==8)
    //if (index==6||index==8)
    {
        [SVProgressHUD showInfoWithStatus:@"暂无可用信息"];
        return NO;
    }
     */
    
    switch (index)
    {
        case 0://首页
        {
            NSArray* targetArray=[viewArray objectForKey:[NSString stringWithFormat:@"%ldPage",(long)index]];
            if(targetArray==nil)
            {
                //targetArray=[NSArray arrayWithObject:[[indexViewController alloc]init]];
                targetArray=[NSArray arrayWithObject:[[NewHomepageViewController alloc]init]];
            }
            
            [main setViewControllers:targetArray animated:NO];
            
            break;
        }
            
        case 1://展会简介
        {
            NSArray* targetArray=[viewArray objectForKey:[NSString stringWithFormat:@"%ldPage",(long)index]];
            if(targetArray==nil)
            {
                targetArray=[NSArray arrayWithObject:[[ExhibitionProfileViewController alloc]init]];
            }
            
            [main setViewControllers:targetArray animated:NO];
            
            break;
        }
            
        case 2://电子会刊
        {
            BOOL is = [[DBProject shareDBProject] isDataBaseUpdating];
            
            if (!is)
            {
                
                NSLog(@"ok");
                
            }
            else
            {
                [SVProgressHUD showInfoWithStatus:@"展商数据更新中..."];
                return NO;
            }
            
            NSArray* targetArray=[viewArray objectForKey:[NSString stringWithFormat:@"%ldPage",(long)index]];
            if(targetArray==nil)
            {
                targetArray=[NSArray arrayWithObject:[[ExhibitorElectronicJournalViewController alloc]init]];
            }
            
            [main setViewControllers:targetArray animated:NO];
            
            break;
        }
            
        case 3://商贸配对
        {
            NSArray* targetArray=[viewArray objectForKey:[NSString stringWithFormat:@"%ldPage",(long)index]];
//            if(targetArray==nil)
//            {
//                if (is)
//                {
//                    targetArray = [NSArray arrayWithObjects:[[BusinessMatchMakingViewController alloc]init],[[XVCReleaseProduct alloc] init], nil];
//                }
//                else
//                {
//                    targetArray=[NSArray arrayWithObject:[[BusinessMatchMakingViewController alloc]init]];
//                }
//            }
            if (is)
                {
                    targetArray = [NSArray arrayWithObjects:[[BusinessMatchMakingViewController alloc]init],[[XVCReleaseProduct alloc] init], nil];
                }
            else
                {
                    targetArray=[NSArray arrayWithObject:[[BusinessMatchMakingViewController alloc]init]];
                }

            [main setViewControllers:targetArray animated:NO];
            
            break;
        }
            
        case 4://消息
        {
            NSArray* targetArray=[viewArray objectForKey:[NSString stringWithFormat:@"%ldPage",(long)index]];
            if(targetArray==nil)
            {
                if ([UserInfo shareUserInfo].isExhibitor)
                {
                    targetArray = [NSArray arrayWithObject:[[XVCMessage alloc] init]];
                }
                else
                {
                    targetArray = [NSArray arrayWithObject:[[XVCVisitorMessage alloc]init]];
                }
            }
            
            [main setViewControllers:targetArray animated:NO];
            
            break;
        }
           
        case 5://收藏夹
        {
            NSArray* targetArray=[viewArray objectForKey:[NSString stringWithFormat:@"%ldPage",(long)index]];
//            if(targetArray==nil)
//            {
//                targetArray=[NSArray arrayWithObject:[[XVCCollectPocket alloc]init]];
//            }
            targetArray=[NSArray arrayWithObject:[[XVCCollectPocket alloc]init]];
            
            [main setViewControllers:targetArray animated:NO];
            
            break;
        }
            
        case 10://扫描二维码
        {
            NSArray* targetArray=[viewArray objectForKey:[NSString stringWithFormat:@"%ldPage",(long)index]];
            if(targetArray==nil)
            {
                targetArray=[NSArray arrayWithObject:[[XVCScanQRCode alloc]init]];
            }
            
            [main setViewControllers:targetArray animated:NO];
            
            break;
        }
            
        case 6: //展会地图
        {
            NSArray* targetArray=[viewArray objectForKey:[NSString stringWithFormat:@"%ldPage",(long)index]];
            if(targetArray==nil)
            {
                targetArray=[NSArray arrayWithObject:[[XVCMapTypeShow alloc]init]];
            }
            [[NSNotificationCenter defaultCenter] postNotificationName:@"ClickMapShow" object:nil];
            [main setViewControllers:targetArray animated:NO];
            
            break;
        }
   
        case 9: //联系我们
        {
            NSArray* targetArray=[viewArray objectForKey:[NSString stringWithFormat:@"%ldPage",(long)index]];
            if(targetArray==nil)
            {
                targetArray=[NSArray arrayWithObject:[[ConnectUsViewController alloc]init]];
            }
            
            [main setViewControllers:targetArray animated:NO];
            
            break;
        }
            
        case 7: //展位预订
        {
            NSArray* targetArray=[viewArray objectForKey:[NSString stringWithFormat:@"%ldPage",(long)index]];
            
            [DKHTTPRequest sendDataWithShortUrl:@"UrlForExhibitor"
                                          Param:[XXTool formatStringWithLanguage:@""]
                                     ImageArray:nil
                                        TimeOut:10
                                     Completion:^BOOL(NSMutableDictionary *serverDic, DKHTTPResponseType bufferType) {
                                         if ([[serverDic objectForKey:@"code"] intValue]==200)
                                         {
                                             [self.exhibitorWeb refreshWebViewWithRequestString:[serverDic objectForKey:@"url"]];
                                         }
                                         
                                         return YES;
                                     } IsBuffering:NO
                              IsNeedReplaceWrap:NO
                                IsUseUpLoadHead:NO];
            
            if(targetArray==nil)
            {
                
                targetArray=[NSArray arrayWithObject:self.exhibitorWeb];
            }
            
            [main setViewControllers:targetArray animated:NO];
            
            break;
        }
            
        case 8: //观众预登记
        {
            NSArray* targetArray=[viewArray objectForKey:[NSString stringWithFormat:@"%ldPage",(long)index]];
            
            [DKHTTPRequest sendDataWithShortUrl:@"UrlForVisitor"
                                          Param:[XXTool formatStringWithLanguage:@""]
                                     ImageArray:nil
                                        TimeOut:10
                                     Completion:^BOOL(NSMutableDictionary *serverDic, DKHTTPResponseType bufferType) {
                                         if ([[serverDic objectForKey:@"code"] intValue]==200)
                                         {
                                             [self.visitorWeb refreshWebViewWithRequestString:[serverDic objectForKey:@"url"]];
                                         }
                                         
                                         return YES;
                                     } IsBuffering:NO
                              IsNeedReplaceWrap:NO
                                IsUseUpLoadHead:NO];
            
            if(targetArray==nil)
            {
                targetArray=[NSArray arrayWithObject:self.visitorWeb];
            }
            
            [main setViewControllers:targetArray animated:NO];
            
            break;
        }
            
        case 11:  //个人资料
        {
            NSArray* targetArray=[viewArray objectForKey:[NSString stringWithFormat:@"%ldPage",(long)index]];
            if(targetArray==nil)
            {
                targetArray=[NSArray arrayWithObject:[[PersonInfoViewController alloc]init]];
            }
            
            [main setViewControllers:targetArray animated:NO];
            
            break;
        }
    }
    
    return YES;
}
-(BOOL)newHomepageView:(NewHomepageViewController*)view didClickIndex:(NSInteger)index isNeedSpecialTreat:(BOOL)is{
    sider = view;
    NSLog(@"pre  里点击的索引是----%ld",index);
    [viewArray setObject:main.viewControllers forKey:[NSString stringWithFormat:@"%ldPage",(long)index]];
    //个人中心、商贸配对、消息、收藏夹、联系我们 需要判断是否登录
    if (index==11||index==3||index==4||index==5||index==8)
    {
        if ([[UserInfo shareUserInfo].userID isEqualToString:@""])
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"提示", nil)
                                                            message:NSLocalizedString(@"您还没有登录,请先登录", nil)
                                                           delegate:self
                                                  cancelButtonTitle:NSLocalizedString(@"取消", nil)
                                                  otherButtonTitles:NSLocalizedString(@"登录", nil), nil];
            [alert show];
            return NO;
        }
    }
    switch (index) {
        case 0:  // 首页
        {
            NSArray* targetArray=[viewArray objectForKey:[NSString stringWithFormat:@"%ldPage",(long)index]];
            if(targetArray==nil)
            {
                //targetArray=[NSArray arrayWithObject:[[indexViewController alloc]init]];
                targetArray=[NSArray arrayWithObject:[[indexViewController alloc]init]];
            }
            
            [main setViewControllers:targetArray animated:NO];
            break;
        }
        case 1:  // 简介
        {
            NSArray* targetArray=[viewArray objectForKey:[NSString stringWithFormat:@"%ldPage",(long)index]];
            if(targetArray==nil)
            {
                targetArray=[NSArray arrayWithObject:[[ExhibitionProfileViewController alloc]init]];
            }
            
            [main setViewControllers:targetArray animated:NO];
             break;
        }
        case 2:  // 会刊
        {
            BOOL is = [[DBProject shareDBProject] isDataBaseUpdating];
            
            if (!is)
            {
                
                NSLog(@"ok");
                
            }
            else
            {
                [SVProgressHUD showInfoWithStatus:@"展商数据更新中..."];
                return NO;
            }
            
            NSArray* targetArray=[viewArray objectForKey:[NSString stringWithFormat:@"%ldPage",(long)index]];
            if(targetArray==nil)
            {
                targetArray=[NSArray arrayWithObject:[[ExhibitorElectronicJournalViewController alloc]init]];
            }
            
            [main setViewControllers:targetArray animated:NO];
            
            break;

        }
        case 3:  // 商贸配对
        {
            NSArray* targetArray=[viewArray objectForKey:[NSString stringWithFormat:@"%ldPage",(long)index]];
            if (is)
            {
                targetArray = [NSArray arrayWithObjects:[[BusinessMatchMakingViewController alloc]init],[[XVCReleaseProduct alloc] init], nil];
            }
            else
            {
                targetArray=[NSArray arrayWithObject:[[BusinessMatchMakingViewController alloc]init]];
            }
            
            [main setViewControllers:targetArray animated:NO];
            
            break;
        }
        case 4:  // 消息
        {
            NSArray* targetArray=[viewArray objectForKey:[NSString stringWithFormat:@"%ldPage",(long)index]];
            if(targetArray==nil)
            {
                if ([UserInfo shareUserInfo].isExhibitor)
                {
                    targetArray = [NSArray arrayWithObject:[[XVCMessage alloc] init]];
                }
                else
                {
                    targetArray = [NSArray arrayWithObject:[[XVCVisitorMessage alloc]init]];
                }
            }
            
            [main setViewControllers:targetArray animated:NO];
            
            break;
        }
        case 5:  // 收藏
        {
            NSArray* targetArray=[viewArray objectForKey:[NSString stringWithFormat:@"%ldPage",(long)index]];
            //            if(targetArray==nil)
            //            {
            //                targetArray=[NSArray arrayWithObject:[[XVCCollectPocket alloc]init]];
            //            }
            targetArray=[NSArray arrayWithObject:[[XVCCollectPocket alloc]init]];
            
            [main setViewControllers:targetArray animated:NO];
            
            break;
        }
        case 6:  // 扫描二维码
        {
            NSArray* targetArray=[viewArray objectForKey:[NSString stringWithFormat:@"%ldPage",(long)index]];
            if(targetArray==nil)
            {
                targetArray=[NSArray arrayWithObject:[[XVCScanQRCode alloc]init]];
            }
            
            [main setViewControllers:targetArray animated:NO];
            
            break;
        }
        case 7:  // 展馆地图
        {
            NSArray* targetArray=[viewArray objectForKey:[NSString stringWithFormat:@"%ldPage",(long)index]];
            if(targetArray==nil)
            {
                targetArray=[NSArray arrayWithObject:[[XVCMapTypeShow alloc]init]];
            }
            [[NSNotificationCenter defaultCenter] postNotificationName:@"ClickMapShow" object:nil];
            [main setViewControllers:targetArray animated:NO];
            
            break;
        }
        case 8:  // 联系我们
        {
            NSArray* targetArray=[viewArray objectForKey:[NSString stringWithFormat:@"%ldPage",(long)index]];
            if(targetArray==nil)
            {
                targetArray=[NSArray arrayWithObject:[[ConnectUsViewController alloc]init]];
            }
            
            [main setViewControllers:targetArray animated:NO];
            
            break;
        }
        case 9:  // 展位登记
        {
            NSArray* targetArray=[viewArray objectForKey:[NSString stringWithFormat:@"%ldPage",(long)index]];
            
            [DKHTTPRequest sendDataWithShortUrl:@"UrlForExhibitor"
                                          Param:[XXTool formatStringWithLanguage:@""]
                                     ImageArray:nil
                                        TimeOut:10
                                     Completion:^BOOL(NSMutableDictionary *serverDic, DKHTTPResponseType bufferType) {
                                         if ([[serverDic objectForKey:@"code"] intValue]==200)
                                         {
                                             [self.exhibitorWeb refreshWebViewWithRequestString:[serverDic objectForKey:@"url"]];
                                         }
                                         
                                         return YES;
                                     } IsBuffering:NO
                              IsNeedReplaceWrap:NO
                                IsUseUpLoadHead:NO];
            
            if(targetArray==nil)
            {
                
                targetArray=[NSArray arrayWithObject:self.exhibitorWeb];
            }
            
            [main setViewControllers:targetArray animated:NO];
            
            break;
        }
        case 10:  // 观众预登记
        {
            NSArray* targetArray=[viewArray objectForKey:[NSString stringWithFormat:@"%ldPage",(long)index]];
            
            [DKHTTPRequest sendDataWithShortUrl:@"UrlForVisitor"
                                          Param:[XXTool formatStringWithLanguage:@""]
                                     ImageArray:nil
                                        TimeOut:10
                                     Completion:^BOOL(NSMutableDictionary *serverDic, DKHTTPResponseType bufferType) {
                                         if ([[serverDic objectForKey:@"code"] intValue]==200)
                                         {
                                             [self.visitorWeb refreshWebViewWithRequestString:[serverDic objectForKey:@"url"]];
                                         }
                                         
                                         return YES;
                                     } IsBuffering:NO
                              IsNeedReplaceWrap:NO
                                IsUseUpLoadHead:NO];
            
            if(targetArray==nil)
            {
                targetArray=[NSArray arrayWithObject:self.visitorWeb];
            }
            
            [main setViewControllers:targetArray animated:NO];
            
            break;
        }
        case 11:  // 我
        {
            NSArray* targetArray=[viewArray objectForKey:[NSString stringWithFormat:@"%ldPage",(long)index]];
            if(targetArray==nil)
            {
                targetArray=[NSArray arrayWithObject:[[PersonInfoViewController alloc]init]];
            }
            
            [main setViewControllers:targetArray animated:NO];
            
            break;
        }
            
        default:
            break;
    }
    
    return YES;
}

#pragma mark alertviewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==1)
    {
        SelectRegistrationStatusViewController *vc = [[SelectRegistrationStatusViewController alloc] init];
        vc.isNeedGoIntro = YES;
        [sider presentViewController:[[MainViewController alloc] initWithRootViewController:vc] animated:YES completion:^{
        }];
    }
}

@end
